import Util from '../../utils/util.js';
import {getShopSearchList} from "../../api/shop";
import {getHotWords} from "../../api/api";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    word: '',
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    limit: 10,
    searchRecord: [],

    isShowHistory: false,
    input_focus: true,

    hotwords: [
      {word: "烤鸭"},
      {word: "皮蛋瘦肉粥"},
      {word: "莲藕排骨汤"},
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    this.openHistorySearch();

    wx.showLoading({
      title: "正在加载..."
    })

    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo,
        isload: false,
        shop_id: options.id
      })
    }).then(() => {
      getHotWords().then((res) => {
        this.setData({
          hotwords: res.data || []
        })
      })
      that.onLoadTabbar();
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    }).finally(() => {
      wx.hideLoading();
    })

    // getShopList()

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  changeHotword(e) {
    // 切换热门词
    const that = this,
      word = e.currentTarget.dataset.word;

    this.setData({
      loadend: false,
      page: 1,
      word: word,
      isShowHistory: false,
      lists: []
    });

    this.getLists();
  },

  getLists() {
    const that = this,
      filter = [];
    let lists = that.data.lists;

    if (that.data.loadend) {
      return;
    }
    if (that.data.loading) {
      return;
    }

    that.setData({loading: true, loadTitle: '加载更多内容'});
    filter['page_num'] = that.data.page;
    if (that.data.word) {
      filter['keywords'] = that.data.word;
    }

    getShopSearchList(filter)
      .then((res) => {
        const list = res.data.items || [];
        const loadend = res.data.last_page <= that.data.page;
        lists = app.SplitArray(list, lists);

        that.setData({
          lists: lists,
          loadend: loadend,
          loading: false,
          loadTitle: loadend ? '我也是有底线的' : '加载更多内容',
          total: res.data.total,
          page: that.data.page + 1,
          et_show: lists.length > 0,
        });
      })
      .catch((errmsg) => {
        app.Tips({title: errmsg});
        that.setData({
          total: 0,
          loading: false,
          loadTitle: errmsg
        });
      }).finally(() => {
      this.setData({
        isLoading: true
      })
    });
  },

  loadMoreList(e) {
    // 点击加载更多
    this.getLists();
  },

  onInput(e) {
    this.setData({
      word: e.detail.value,
      isLoading: false
    });
  },
  confirmInput(e) {
    const inputVal = e.detail.value || this.data.word;

    this.setData({
      loadend: false,
      page: 1,
      word: inputVal,
      isShowHistory: false,
      lists: []
    });

    this.getLists();

    if (inputVal) {
      const searchRecord = this.data.searchRecord,
        arrnum = searchRecord.indexOf(inputVal);

      if (arrnum != -1) {
        // 删除已存在后重新插入至数组
        searchRecord.splice(arrnum, 1);
      }

      if (searchRecord.length < 5) {
        // 将搜索值放入历史记录中,只能放前五条
        searchRecord.unshift(inputVal);
      } else {
        searchRecord.pop(); // 删掉旧的时间最早的第一条
        searchRecord.unshift(inputVal);
      }

      // 将历史记录数组整体储存到缓存中
      wx.setStorageSync('searchRecord', searchRecord);
    }
  },

  openHistorySearch: function () {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [] // 若无储存则为空
    });
  },

  clearSearchRecord(e) {
    // 清理搜索历史
    Util.showModelTip("清楚历史记录", ' ').then(res => {
      if (res.confirm) {
        wx.clearStorageSync('searhRecord');
        this.setData({
          searchRecord: []
        });
      }
    })
  },

  removeSearchRecord(e) {   // 删除指定搜索记录

    const that = this,
      searchRecord = that.data.searchRecord,
      key = e.currentTarget.dataset.k;

    searchRecord.splice(key, 1);

    this.setData({
      searchRecord: searchRecord || []
    });

    // 将历史记录数组整体储存到缓存中
    wx.setStorageSync('searchRecord', searchRecord || []);
  },

  onClear() {
    this.setData({
      word: ''
    });
  },

  inputFocus(e) {
    this.openHistorySearch();
    this.setData({
      isShowHistory: true,
      input_focus: true
    });
  },

  inputBlur(e) {
    this.setData({
      input_focus: false
    });
  },

  onPullDownRefresh: function () {
    this.setData({
      loadend: false,
      page: 1,
      lists: []
    });
    this.getLists();
  },

  onLoadTabbar() {//底部tabbar返回数据
    const that = this
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({isIphoneX: res})
    })
  },

  onReachBottom: function () {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

});
